package com.shi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.shi.annotation.Auth;
import com.shi.annotation.MyLog;
import com.shi.common.Result;
import com.shi.entity.dto.RoleEditDto;
import com.shi.entity.po.Role;
import com.shi.enums.AuthCodeEnum;
import com.shi.enums.LogType;
import com.shi.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Role)表控制层
 *
 * @author makejava
 * @since 2024-05-16 17:20:29
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;


    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */
    @ApiOperation("查询全部角色")
    @GetMapping("/all")
    @Auth(hasPerm = {AuthCodeEnum.PERM_ROLE_SELECT,AuthCodeEnum.PERM_ACCOUNT_GIVE_ROLES})
    public Result list() {
        List<Role> list = roleService.list(new LambdaQueryWrapper<Role>().orderByDesc(Role::getCreateTime));
        return Result.ok(list);
    }

    @ApiOperation("条件查询")
    @GetMapping("/like")
    @Auth(hasPerm = {AuthCodeEnum.PERM_ROLE_SELECT,AuthCodeEnum.PERM_ACCOUNT_GIVE_ROLES})
    public Result listByCondition(@RequestParam("like") String likeProp){
        List<Role> list=roleService.listByCondition(likeProp);
        return Result.ok(list);
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("通过id查询角色")
    @GetMapping("/{id}")
    @Auth(hasPerm = AuthCodeEnum.PERM_ROLE_SELECT)
    public Result getById(@PathVariable("id") Integer id) {
        Role role = roleService.getById(id);
        return Result.ok(role);
    }

    /**
     * 新增数据
     *
     * @param role 实体
     * @return 新增结果
     */
    @ApiOperation("添加角色")
    @ApiOperationSupport(ignoreParameters = {"roleId", "createTime"})
    @MyLog(type = LogType.INSERT)
    @PostMapping
    @Auth(hasPerm = AuthCodeEnum.PERM_ROLE_INSERT_DELETE_UPDATE)
    public Result add(@RequestBody Role role) {
        return Result.ok(this.roleService.add(role));
    }

    /**
     * 编辑数据
     *
     * @param roleEditDto 实体
     * @return 编辑结果
     */
    @ApiOperation("为角色分配菜单")
    @PostMapping("/giveMenu")
    @Auth(hasPerm = AuthCodeEnum.PERM_ROLE_GIVE_MENUS)
    @MyLog(type = LogType.OTHER)
    public Result giveMenus(@RequestBody RoleEditDto roleEditDto) {
        roleService.giveMenu(roleEditDto);
        return Result.ok();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @ApiOperation("通过id删除角色")
    @DeleteMapping("/{roleId}")
    @Auth(hasPerm = AuthCodeEnum.PERM_ROLE_INSERT_DELETE_UPDATE)
    @MyLog(type = LogType.DELETE)
    public Result removeById(@PathVariable("roleId") Integer id) {
        roleService.removeById(id);
        return Result.ok();
    }

}

